/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jsnapshot;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.event.MouseInputAdapter;

/**
 *
 * @author kipe
 */
public class SelectionArea extends JLabel {

    Rectangle currentRect = null;
    Rectangle rectToDraw = null;
    Rectangle previousRectDrawn = new Rectangle();
    int x1,y1,x2,y2;

    public SelectionArea(ImageIcon image) {
        super(image);
//        setOpaque(true);
//        setMinimumSize(new Dimension(10, 10));

        MyListener myListener = new MyListener();
        addMouseListener(myListener);
        addMouseMotionListener(myListener);
    }

    private class MyListener extends MouseInputAdapter {

        @Override
        public void mouseDragged(MouseEvent e) {
            updateSize(e);
        }

        @Override
        public void mousePressed(MouseEvent e) {
            x1 = e.getX();
            y1 = e.getY();
            currentRect = new Rectangle(x1, y1, 0, 0);
            updateDrawableRect(getWidth(), getHeight());
            repaint();
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            updateSize(e);
            x2 = e.getX();
            y2 = e.getY();
            
            CornerPoints cp = new CornerPoints(x1, y1, x2, y2);
            JsnapshotApp.getApplication().getView().getWin().dispose();
            int x = cp.getX();
            int y = cp.getY();
            int w = cp.getW();
            int h = cp.getH();
            // cropping full screen image
            JsnapshotApp.getApplication().getView().setImage(
                    JsnapshotApp.getApplication().getView().getImage().getSubimage(x, y, w, h));
            JsnapshotApp.getApplication().getView().setPreviewImage(
                    JsnapshotApp.getApplication().getView().getImage());
        }

        void updateSize(MouseEvent e) {
            int x = e.getX();
            int y = e.getY();
            currentRect.setSize(x - currentRect.x, y - currentRect.y);
            updateDrawableRect(getWidth(), getHeight());
            Rectangle totalRepaint = rectToDraw.union(previousRectDrawn);
            repaint(totalRepaint.x, totalRepaint.y, totalRepaint.width,
                    totalRepaint.height);
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g); //paints the background and image

        //If currentRect exists, paint a box on top.
        if (currentRect != null) {
            //Draw a rectangle on top of the image.
            g.setXORMode(Color.white); //Color of line varies
            //depending on image colors
            g.drawRect(rectToDraw.x, rectToDraw.y, rectToDraw.width - 1,
                    rectToDraw.height - 1);

//            controller.updateLabel(rectToDraw);
        }
    }

    private void updateDrawableRect(int compWidth, int compHeight) {
        int x = currentRect.x;
        int y = currentRect.y;
        int width = currentRect.width;
        int height = currentRect.height;

        //Make the width and height positive, if necessary.
        if (width < 0) {
            width = 0 - width;
            x = x - width + 1;
            if (x < 0) {
                width += x;
                x = 0;
            }
        }
        if (height < 0) {
            height = 0 - height;
            y = y - height + 1;
            if (y < 0) {
                height += y;
                y = 0;
            }
        }

        //The rectangle shouldn't extend past the drawing area.
        if ((x + width) > compWidth) {
            width = compWidth - x;
        }
        if ((y + height) > compHeight) {
            height = compHeight - y;
        }

        //Update rectToDraw after saving old value.
        if (rectToDraw != null) {
            previousRectDrawn.setBounds(rectToDraw.x, rectToDraw.y,
                    rectToDraw.width, rectToDraw.height);
            rectToDraw.setBounds(x, y, width, height);
        } else {
            rectToDraw = new Rectangle(x, y, width, height);
        }
    }
}
